package com.mjfeed.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import com.mjfeed.interceptor.MemberInterceptor;

/**
 * @description 会员
 * @author zhaominglei
 * @date 2014年10月7日
 */
@Entity
@Table(name="feed_member")
public class Member extends BaseEntity implements Serializable {
	private static final long serialVersionUID = 3342734254653522329L;

	/** "身份信息"参数名称 */
	public static final String PRINCIPAL_ATTRIBUTE_NAME = MemberInterceptor.class.getName() + ".PRINCIPAL";

	/** "用户名"Cookie名称 */
	public static final String USERNAME_COOKIE_NAME = "username";
	
	/** "姓名"Cookie名称 */
	public static final String NAME_COOKIE_NAME = "name";
	
	/** 最大收藏商品数 */
	public static final Integer MAX_FAVORITE_COUNT = 50;
	
	/** 用户名(即手机号) */
	private String username;

	/** 密码 */
	private String password;

	/** 邮箱 */
	private String email;
	
	/** 姓名 */
	private String name;

	/** 手机 */
	private String mobile;
	
	/** 是否可用 */
	private Boolean isEnabled;

	/** 是否锁定 */
	private Boolean isLocked;
	
	/** 锁定日期 */
	private Date lockedDate;
	
	/** 登录日期 */
	private Date loginDate;
	
	/** 登录失败次数 */
	private Integer loginFailureCount;

	/** 登录ip */
	private String loginIp;

	/** 注册ip */
	private String registerIp;

	/** 会员等级 */
//	private MemberRank memberRank;
	
	/** 购物车 */
	private Cart cart;

	/** 订单 */
	private Set<Order> orders = new HashSet<Order>();
	
	/** 收货地址 */
	private Set<Receiver> receivers = new HashSet<Receiver>();
	
	/** 收藏商品 */
	private Set<Product> favoriteProducts = new HashSet<Product>();
	
	/** 评论 */
	private Set<Comment> comments = new HashSet<Comment>();
	
	/** 论坛话题 */
    private Set<ForumTopic> forumTopics = new HashSet<ForumTopic>();
    
    /** 论坛回复 */
    private Set<ForumReply> forumReplies = new HashSet<ForumReply>();
    
    /** 浏览日志*/
    private Set<BrowseLog> browseLogs = new HashSet<BrowseLog>();
	
	@Column(nullable = false, updatable = false, unique = true, length = 100)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Column(nullable = false)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(nullable = false)
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	@Column(name="is_enabled", columnDefinition="BIT")
	public Boolean getIsEnabled() {
		return isEnabled;
	}

	public void setIsEnabled(Boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	@Column(name="is_locked", columnDefinition="BIT")
	public Boolean getIsLocked() {
		return isLocked;
	}

	public void setIsLocked(Boolean isLocked) {
		this.isLocked = isLocked;
	}

	@Column(name="locked_date")
	public Date getLockedDate() {
		return lockedDate;
	}

	public void setLockedDate(Date lockedDate) {
		this.lockedDate = lockedDate;
	}

	@Column(name="login_date")
	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	@Column(name="login_failure_count")
	public Integer getLoginFailureCount() {
		return loginFailureCount;
	}

	public void setLoginFailureCount(Integer loginFailureCount) {
		this.loginFailureCount = loginFailureCount;
	}

	@Column(name="login_ip")
	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}
	
	@Column(name="register_ip")
	public String getRegisterIp() {
		return registerIp;
	}

	public void setRegisterIp(String registerIp) {
		this.registerIp = registerIp;
	}

	@OneToOne(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
	public Cart getCart() {
		return cart;
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
	public Set<Order> getOrders() {
		return orders;
	}

	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}
	
	@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
	@OrderBy("isDefault desc, createDate desc")
	public Set<Receiver> getReceivers() {
		return receivers;
	}

	public void setReceivers(Set<Receiver> receivers) {
		this.receivers = receivers;
	}
	
	@ManyToMany(fetch=FetchType.LAZY)
	@JoinTable(name="feed_favorite_product", joinColumns = { @JoinColumn(name = "member_id", updatable = false) },	inverseJoinColumns = { @JoinColumn(name = "product_id", updatable = false) })
	public Set<Product> getFavoriteProducts() {
		return favoriteProducts;
	}

	public void setFavoriteProducts(Set<Product> favoriteProducts) {
		this.favoriteProducts = favoriteProducts;
	}
	
	@OneToMany(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
	@OrderBy("isShow desc, createDate desc")
	public Set<Comment> getComments() {
		return comments;
	}

	public void setComments(Set<Comment> comments) {
		this.comments = comments;
	}
	
	@OneToMany(mappedBy="member", fetch=FetchType.LAZY)
	@OrderBy("id desc")
	public Set<ForumTopic> getForumTopics() {
		return forumTopics;
	}

	public void setForumTopics(Set<ForumTopic> forumTopics) {
		this.forumTopics = forumTopics;
	}
	
	@OneToMany(mappedBy="member", fetch=FetchType.LAZY)
	@OrderBy("id desc")
	public Set<ForumReply> getForumReplies() {
		return forumReplies;
	}

	public void setForumReplies(Set<ForumReply> forumReplies) {
		this.forumReplies = forumReplies;
	}
	
	@OneToMany(mappedBy="member", fetch=FetchType.LAZY)
	@OrderBy("id desc")
	public Set<BrowseLog> getBrowseLogs() {
		return browseLogs;
	}

	public void setBrowseLogs(Set<BrowseLog> browseLogs) {
		this.browseLogs = browseLogs;
	}
	
	@Transient
	public String getShowname(){
		if (StringUtils.isNotBlank(getName())) {
			return getName();
		} else {
			return getUsername();
		}
	}
	
	/**
	 * 持久化前处理
	 */
	@PrePersist
	public void prePersist() {
		if (getUsername() != null) {
			setMobile(getUsername());
		}
	}
	
	/**
	 * 更新前处理
	 */
	@PreUpdate
	public void preUpdate() {
		if (getUsername() != null) {
			setMobile(getUsername());
		}
	}
}
